#include "stdafx.h"
#include "OpenGLWrapper.h"
OpenGLWrapper::OpenGLWrapper()
{
    reset();
}

OpenGLWrapper::~OpenGLWrapper()
{
    purge();
}

void OpenGLWrapper::init(HWND hWnd)
{
    // remember the window handle (HWND)
    mhWnd = hWnd;
    // get the device context (DC)
    mhDC = GetDC( mhWnd );

    // set the pixel format for the DC
    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory( &pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
                    PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    int format = ChoosePixelFormat( mhDC, &pfd );
    SetPixelFormat( mhDC, format, &pfd );
	InitGLExtentions();
    // create the render context (RC)
    mhRC = wglCreateContext( mhDC );
	
            

    wglMakeCurrent( mhDC, mhRC );
	glEnable(GL_DEPTH);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);
	glShadeModel(GL_SMOOTH);
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;

	
	glClearColor(0.5f,0.5f,0.5f,1.0f);                   // background color

	glClearStencil(0);                          // clear stencil buffer
    
    // make it the current render context
	
	
}

void OpenGLWrapper::purge()
{
    if ( mhRC )
    {
        wglMakeCurrent( NULL, NULL );
        wglDeleteContext( mhRC );
    }
    if ( mhWnd && mhDC )
    {
        ReleaseDC( mhWnd, mhDC );
    }
    reset();
}


void OpenGLWrapper::reset()
{
    mhWnd = NULL;
    mhDC = NULL;
    mhRC = NULL;
}